package iopera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2024-07-13
 * Time: 14:24
 */
public class AddIOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");

        //1.判满
        int currentSized = bookList.getUsedSized();
        if(currentSized == bookList.getBooks().length){
            System.out.println("书架满了");
            return;
        }

        //2.构建对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入图书名字：");
        String name = sc.nextLine();
        System.out.println("请输入图书作者：");
        String author = sc.nextLine();
        System.out.println("请输入图书类型：");
        String type = sc.nextLine();
        System.out.println("请输入图书价格：");
        int price = sc.nextInt();

        //3.判断书架有没有这本书
        Book newBook = new Book(name,author,price,type);

        for (int i = 0; i < bookList.getUsedSized(); i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("这本书已经有了");
                return;
            };
        }

        //4.插入这本书
        bookList.setBooks(currentSized,newBook);
        bookList.setUsedSized(currentSized + 1);
        System.out.println("新增图书成功");;
    }

}
